<?php

class Client_Table_App extends Apk_Db_Table
{
	protected $_name      = 'app';
    protected $_primary   = 'id';
    protected $_ERROR = '';

    public function GetError()
    {
    	return $this->_ERROR;
    }


    public function GetDataRow($id)
    {
    	$sel = $this->getAdapter()->select()
    				->from($this->_name)
    				->where('id = ?', $id);

    	return $this->setDecorator('simpleDecorator')->getList($sel)->getRow(0)->toArray();
    }


    //*******************
    // insert/update/delete

	public function insert(array $data)
    {
    	$data['add_date'] = time();

    	try{
			return parent::insert($data);
		}catch(Exception $e)
		{
			echo $e->__toString(); die();
		}
    }

    public function update(array $data, $id, $user_id)
    {
    	$data['modify_date'] = time();
    	$data['modify_user_id'] = $user_id;
    	$data['is_ghost'] = 0;

    	if(!isset($data['is_active'])) $data['is_active'] = 0;

		try{
			return parent::update($data, 'id = '.$id);
		}catch(Exception $e)
		{
			echo $e->__toString(); die();
		}
    }


    public function remove($items, $user_id)
    {
    	foreach($items as $k => $id)
    	{
	    	$data['modify_user_id'] = $user_id;
	    	$data['modify_date'] = time();
	    	$data['is_removed'] = 1;

	    	parent::update($data, 'id = '.$id);
    	}

    	return true;
	}



    /********************
     ***  Decorators  ***
    *********************/

    public function simpleDecorator($key, $value)
    {
        switch ($key) {
            case 'public_date':
            case 'modify_date':
            case 'add_date':
            	$result[$key]['__attributes'] = $this->timestampToArray($value);
            	break;
            default:
                $result[$key] = stripslashes($value);
                break;
        }
        return $result;
    }



}
?>